package com.admin.service.ipl;

import com.admin.mapper.AdsensitiveMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.atri.pojos.ApArticle;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AdsensitiveService extends ServiceImpl<AdsensitiveMapper, AdSensitive> implements com.admin.service.AdsensitiveService {

    @Resource
    AdsensitiveMapper adsensitiveMapper;


    @Override
    public ResponseResult sensitiveqc(SensitiveDto dto) {
//        判断这个dto是否有数据
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        页码判断 没有的话 初始化 并赋值
        dto.checkParam();
        Page page=new Page(dto.getPage(),dto.getSize());
//        条件查询
        LambdaQueryWrapper<AdSensitive> qw=new LambdaQueryWrapper<>();
        qw.like(dto.getName()!=null,AdSensitive::getSensitives,dto.getName());
        IPage l=page(page,qw);
//向上转型
        ResponseResult r=new PageResponseResult(dto.getPage(),dto.getSize(),(int)l.getTotal());
//      存储数据
       r.setData(l.getRecords());
        return r;
    }

    @Override
    public ResponseResult saveAdSensitive(AdSensitive adSensitive) {
        //        判断参数是否为空
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //        添加时间 并保存
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
//        判断参数是否为空
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        直接修改
        adSensitive.setCreatedTime(new Date());
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
//        判断是否传值
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        不需要别的条件 直接删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

//    敏感词全查
    @Override
    public List<AdSensitive> findAll() {

        return adsensitiveMapper.selectList(null);
    }
}
